import {createRouter, createWebHistory} from 'vue-router'
import NProgress from "nprogress";
import {userStorei} from "@/stores/user_store";
import {Message} from "@arco-design/web-vue";
import {showLogin} from "@/components/web/f_login"; // 导入 nprogress模块


const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            name: "web",
            path: "/",
            component: () => import("@/views/web/index.vue"),
            // redirect: "/admin",
            children: [
                {
                    name: "webIndex",
                    path: "",
                    component: () => import("@/views/web/web.vue"),
                },
                {
                    name: "history",
                    path: "history",
                    component: () => import("@/views/web/history.vue"),
                    meta: {
                        title: "历史会话",
                        role: [1, 2, 3]
                    },
                },
                {
                    name: "squareBase",
                    path: "square",
                    component: () => import("@/views/web/square/index.vue"),
                    children: [
                        {
                            name: "square",
                            path: "",
                            component: () => import("@/views/web/square/square.vue"),
                        },
                        {
                            name: "myRole",
                            path: "my",
                            component: () => import("@/views/web/square/my_role.vue"),
                            meta: {
                                title: "我的角色",
                                role: [1, 2, 3]
                            },
                        },
                    ]
                },
                {
                    name: "chat",
                    path: "chat/:id",
                    component: () => import("@/views/web/chat.vue"),
                    meta: {
                        title: "对话",
                        role: [1, 2, 3]
                    },
                }
            ]
        },
        {
            name: "login",
            path: "/login",
            component: () => import("@/views/login/index.vue")
        },
        {
            name: "admin",
            path: "/admin",
            component: () => import("@/views/admin/index.vue"),
            meta: {
                title: "首页",
                role: [1, 2, 3]
            },
            children: [
                {
                    name: "home",
                    path: "",
                    component: () => import("@/views/admin/home/index.vue"),
                    meta: {
                        title: "首页",
                    }
                },
                {
                    name: "userCenter",
                    path: "user_center",
                    meta: {
                        title: "个人中心",
                    },
                    children: [
                        {
                            name: "userInfo",
                            path: "user_info",
                            meta: {
                                title: "个人信息"
                            },
                            component: () => import("@/views/admin/user_center/index.vue"),
                        },
                        {
                            name: "userLoginList",
                            path: "user_login_list",
                            meta: {
                                title: "登录列表"
                            },
                            component: () => import("@/views/admin/user_center/user_login_list.vue"),
                        },
                        {
                            name: "scopeRecharge",
                            path: "scope_recharge",
                            meta: {
                                title: "积分充值"
                            },
                            component: () => import("@/views/admin/user_center/scope_recharge.vue"),
                        }
                    ]
                },
                {
                    name: "userManage",
                    path: "user_manage",
                    meta: {
                        title: "用户管理",
                        role: [1]
                    },
                    children: [
                        {
                            name: "userList",
                            path: "user_list",
                            meta: {
                                title: "用户列表"
                            },
                            component: () => import("@/views/admin/user_manage/user_list.vue"),
                        },
                        {
                            name: "orderList",
                            path: "order_list",
                            meta: {
                                title: "订单列表"
                            },
                            component: () => import("@/views/admin/user_manage/order_list.vue"),
                        },
                        {
                            name: "userAdminLoginList",
                            path: "user_admin_login_list",
                            meta: {
                                title: "登录列表"
                            },
                            component: () => import("@/views/admin/user_manage/user_admin_login_list.vue"),
                        }
                    ]
                },
                {
                    name: "aiManage",
                    path: "ai_manage",
                    meta: {
                        title: "AI管理",
                        role: [1, 2]
                    },
                    children: [
                        {
                            name: "roleList",
                            path: "role_list",
                            meta: {
                                title: "角色列表",
                                role: [1]
                            },
                            component: () => import("@/views/admin/ai_manage/role_list.vue"),
                        },
                        {
                            name: "roleOrderList",
                            path: "role_order_list",
                            meta: {
                                title: "工单列表",
                                role: [1]
                            },
                            component: () => import("@/views/admin/ai_manage/role_order_list.vue"),
                        },
                        {
                            name: "sessionList",
                            path: "session_list",
                            meta: {
                                title: "会话列表",
                                role: [1]
                            },
                            component: () => import("@/views/admin/ai_manage/session_list.vue"),
                        },
                        {
                            name: "myRoleList",
                            path: "my_role_list",
                            meta: {
                                title: "我的角色"
                            },
                            component: () => import("@/views/admin/ai_manage/my_role_list.vue"),
                        },
                        {
                            name: "myRoleOrderList",
                            path: "my_role_order_list",
                            meta: {
                                title: "我的工单"
                            },
                            component: () => import("@/views/admin/ai_manage/my_role_order_list.vue"),
                        },

                        {
                            name: "mySessionList",
                            path: "my_session_list",
                            meta: {
                                title: "我的会话"
                            },
                            component: () => import("@/views/admin/ai_manage/my_session_list.vue"),
                        },
                        {
                            name: "myOrderList",
                            path: "my_order_list",
                            meta: {
                                title: "我的订单"
                            },
                            component: () => import("@/views/admin/ai_manage/my_order_list.vue"),
                        }
                    ]
                },
                {
                    name: "settingsManage",
                    path: "settings_manage",
                    meta: {
                        title: "系统配置",
                        role: [1]
                    },
                    children: [
                        {
                            name: "logList",
                            path: "log_list",
                            meta: {
                                title: "日志列表"
                            },
                            component: () => import("@/views/admin/settings_manage/log_list.vue"),
                        },
                        {
                            name: "imageList",
                            path: "image_list",
                            meta: {
                                title: "图片管理"
                            },
                            component: () => import("@/views/admin/settings_manage/image_list.vue"),
                        },
                        {
                            name: "settings",
                            path: "settings",
                            meta: {
                                title: "站点设置"
                            },
                            component: () => import("@/views/admin/settings_manage/settings/index.vue"),
                            children: [
                                {
                                    name: "site_info",
                                    path: "site_info",
                                    meta: {
                                        title: "网站设置"
                                    },
                                    component: () => import("@/views/admin/settings_manage/settings/site_info.vue"),
                                },
                                {
                                    name: "ai",
                                    path: "ai",
                                    meta: {
                                        title: "AI设置"
                                    },
                                    component: () => import("@/views/admin/settings_manage/settings/ai.vue"),
                                },
                                {
                                    name: "wx_login",
                                    path: "wx_login",
                                    meta: {
                                        title: "微信登陆"
                                    },
                                    component: () => import("@/views/admin/settings_manage/settings/wx_login.vue"),
                                },
                                {
                                    name: "wx_pay",
                                    path: "wx_pay",
                                    meta: {
                                        title: "阿里支付"
                                    },
                                    component: () => import("@/views/admin/settings_manage/settings/ali_pay.vue"),
                                },
                                {
                                    name: "email",
                                    path: "email",
                                    meta: {
                                        title: "邮箱设置"
                                    },
                                    component: () => import("@/views/admin/settings_manage/settings/email.vue"),
                                },
                                {
                                    name: "ocr",
                                    path: "ocr",
                                    meta: {
                                        title: "OCR设置"
                                    },
                                    component: () => import("@/views/admin/settings_manage/settings/ocr.vue"),
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            name: "test",
            path: "/test",
            component: () => import("@/test/a-image.vue"),
        },
        {
            name: "notfound",
            path: "/:match(.*)",
            component: () => import("@/views/web/404.vue"),
        }
    ]
})

router.beforeEach((to, from, next) => {
    if (to.meta.role) {

        console.log('Required Roles:', to.meta.role); // 打印路由要求的角色
        // 判断当前用户的角色，在不在列表里面
        const store = userStorei()
        if (!store.isLogin) {
            // 没有登陆
            Message.warning("需要登陆")
            showLogin({isReload: true, to: to.path})
            // router.push({
            //     name: "login", query: {
            //         redirect: to.path
            //     }
            // })
            return
        }

        if (!to.meta.role.includes(store.userInfo.role)) {
            // 不在
            Message.warning("鉴权失败")
            router.push(from.path)
            return
        }
    }
    NProgress.start();//开启进度条
    next()
})
//当路由进入后：关闭进度条
router.afterEach(() => {
    // 在即将进入新的页面组件前，关闭掉进度条
    NProgress.done()//完成进度条
})

export default router
